<?php
// +-------------------------------------------------------------------------------
// | DingZone [ A Rapid Development Framework For Dingtalk ]
// +-------------------------------------------------------------------------------
// | 企业模块权限配置 v1.0
// |
// | Copyright (c) 2014-2018 http://www.heimalanshi.com All rights reserved.
// | Author : Jie
// | Date : 2018-08-09
// +-------------------------------------------------------------------------------

namespace app\core\model;

use think\Cache;
use think\Db;

class EnterpriseModuleAuthority {

    private static function _k($eid, $mid) {
        return "core.EnterpriseModuleAuthority." . $eid . "." . $mid;
    }

    public static function groupAuthorities($authorities) {
        $group = [];
        foreach ($authorities as $key => $authority) {
            if (!isset($group[$authority['group']])) {
                $group[$authority['group']] = [];
            }
            $group[$authority['group']][$key] = $authority;
        }
        return $group;
    }

    /**
     * 获取所有权限点
     *
     * @param $eid
     * @param $mid
     * @return false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    public static function getAll($eid, $mid) {
        $cacheKey = self::_k($eid, $mid);
        $authorities = Cache::get($cacheKey);
        if (empty($authorities)) {
            $authorities = self::_q()->where([
                'enterprise_id' => $eid,
                'module_id' => $mid,
            ])->select();
            $authorities = remap($authorities, "key");

            $moduleAuthorities = SiteModuleAuthority::getAuthorities($mid);
            foreach ($moduleAuthorities as $idx => $moduleAuthority) {
                $key = $moduleAuthority['key'];
                $key = strtolower($key);
                if (!isset($authorities[$key])) {
                    $value = [];
                    if ($moduleAuthority['defaultval'] == 'adminOnly') {
                        $value = [
                            'entAdmin' => true,
                            'moduleAdmin' => true,
                            'other' => '0'
                        ];
                    } else if ($moduleAuthority['defaultval'] == '*') {
                        $value = [
                            'entAdmin' => true,
                            'moduleAdmin' => true,
                            'other' => '*'
                        ];
                    } else {
                        $value = json_decode($moduleAuthority['defaultval'], true);
                    }
                    $authorities[$key] = [
                        'value' => $value
                    ];
                }
                $authorities[$key]['title'] = $moduleAuthority['title'];
                $authorities[$key]['group'] = $moduleAuthority['group'];
                $authorities[$key]['type'] = $moduleAuthority['type'];
                $authorities[$key]['value'] = is_array($authorities[$key]['value']) ?
                    $authorities[$key]['value'] : json_decode($authorities[$key]['value'], true);
                if (!isset($authorities[$key]['value']['entAdmin'])) {
                    $authorities[$key]['value']['entAdmin'] = true;
                }
                if (!isset($authorities[$key]['value']['moduleAdmin'])) {
                    $authorities[$key]['value']['moduleAdmin'] = true;
                }
                if (!isset($authorities[$key]['value']['other'])) {
                    $authorities[$key]['value']['other'] = false;
                }
            }

            Cache::set($cacheKey, $authorities);
        }
        return $authorities;
    }

    /**
     * 添加一个权限点
     *
     * @param $eid
     * @param $mid
     * @param $key
     * @param $value
     * @return int|string
     */
    public static function upsert($eid, $mid, $key, $value) {
        if (is_array($value)) {
            $value = json_encode($value);
        }
        $ret = self::_q()->insertGetId([
            'enterprise_id' => $eid,
            'module_id' => $mid,
            'key' => $key,
            'value' => $value
        ], true);
        $key = self::_k($eid, $mid);
        Cache::rm($key);
        return $ret;
    }

    /**
     * 删除模块相关的所有权限设置
     *
     * @param $mid
     * @return int
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public static function deleteByMid($mid) {
        return self::_q()->where(['module_id' => $mid])->delete();
    }

    /**
     * 查询对象
     *
     * @return \think\db\Query
     */
    private static function _q() {
        return Db::name("EnterpriseModuleAuthority");
    }
}